package com.jgp.glmeeting.mobile.controller;

import com.alibaba.fastjson.JSON;
import com.jgp.glmeeting.mobile.commons.BaseResponse;
import com.jgp.glmeeting.mobile.commons.ErrorMsg;
import com.jgp.glmeeting.model.Participants;
import com.jgp.glmeeting.service.ParticipantsService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * 手机端有关参会人员的相关接口
 */
@RestController
@RequestMapping("/mobile/MobileParticipantsController")
public class MobileParticipantsController {

    private final Logger logger = LoggerFactory.getLogger(MobileParticipantsController.class);

    @Autowired
    private ParticipantsService participantsService;

    /**
     * 查找人员
     * @param participantsId
     * @return
     */
    @RequestMapping("queryById")
    public BaseResponse queryById(String participantsId) {
        if(StringUtils.isBlank(participantsId)) {
            return new BaseResponse(ErrorMsg.INVALID_PARAM.getCode(), ErrorMsg.INVALID_PARAM.getErrorMsg());
        }
        try {
            Participants participants = participantsService.queryById(participantsId);
            BaseResponse baseResponse = new BaseResponse(200);
            baseResponse.setObj(participants);
            return baseResponse;
        }catch (Exception e){
            logger.error("查找人员接口异常, 参数 participantsId={}",e, participantsId);
            return new BaseResponse(ErrorMsg.SERVER_ERROR.getCode(), ErrorMsg.SERVER_ERROR.getErrorMsg());
        }
    }


    /**
     * 修改人员信息
     * @param participants
     * @return
     */
    @RequestMapping("editParticipants")
    public BaseResponse editParticipants(Participants participants){
        if(participants == null || StringUtils.isBlank(participants.getId())) {
            return new BaseResponse(ErrorMsg.INVALID_PARAM.getCode(), ErrorMsg.INVALID_PARAM.getErrorMsg());
        }
        try {
            //更新信息
            participantsService.editParticipantsForMobile(participants);
            BaseResponse baseResponse = new BaseResponse(200);
            baseResponse.setObj(participants);
            return baseResponse;
        }catch (Exception e){
            logger.error("修改人员信息接口异常, 参数 participants={}",e, JSON.toJSON(participants));
            return new BaseResponse(ErrorMsg.SERVER_ERROR.getCode(), ErrorMsg.SERVER_ERROR.getErrorMsg());
        }
    }




    /**
     * 签到接口
     * @param participantsIds
     * @return
     */
    @RequestMapping("sign")
    public BaseResponse sign(String participantsIds, String group){
        if(StringUtils.isBlank(participantsIds) || StringUtils.isBlank(group)) {
            return new BaseResponse(ErrorMsg.INVALID_PARAM.getCode(), ErrorMsg.INVALID_PARAM.getErrorMsg());
        }
        try {
            List<String> participantsIdList = Arrays.asList(participantsIds.split(","));
            List<Participants> list = participantsService.sign(participantsIdList, group);
            BaseResponse baseResponse = new BaseResponse(200);
            baseResponse.setObj(list);
            return baseResponse;
        }catch (Exception e){
            logger.error("签到接口异常, 参数 participantsIds={}",e, participantsIds);
            return new BaseResponse(ErrorMsg.SERVER_ERROR.getCode(), ErrorMsg.SERVER_ERROR.getErrorMsg());
        }

    }


    /**
     * 查询同组需要签到的人员信息
     * @param group
     * @return
     */
    @RequestMapping("querySignInfo")
    public BaseResponse querySignInfo(String group){
        if(StringUtils.isBlank(group)) {
            return new BaseResponse(ErrorMsg.INVALID_PARAM.getCode(), ErrorMsg.INVALID_PARAM.getErrorMsg());
        }
        try {
            List<Participants> participantsList = participantsService.queryListByGroupAndId(null,group);
            BaseResponse baseResponse = new BaseResponse(200);
            baseResponse.setObj(participantsList);
            return baseResponse;
        }catch (Exception e){
            logger.error("查询同组需要签到的人员信息异常, 参数 group={}",e, group);
            return new BaseResponse(ErrorMsg.SERVER_ERROR.getCode(), ErrorMsg.SERVER_ERROR.getErrorMsg());
        }

    }
}
